/*
 * Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef LAUNCHER_H
#define LAUNCHER_H

#ifdef CNCNET_CLIENT
#include "../QtClient/client.hpp"
#endif

#define CNCNET_ERROR_NO_ERROR               0x00
#define CNCNET_ERROR_FILE_NOT_FOUND         0x01
#define CNCNET_ERROR_FAILED_TO_LAUNCH       0x02 /* launching the game exe failed, the file is not an exe file? */
#define CNCNET_ERROR_INVALID_PROCESS        0x03 /* none of the spawned processes were found to be a valid game */
#define CNCNET_ERROR_UNKNOWN                0xFF

struct hook
{
    uint32_t    addr;
    void        (*func)(CONTEXT *);
#if _DEBUG
    unsigned int cnt;
#endif
};

#define MAX_HOOKS   48

/* thipx32 hooks */
void hook_IPX_Initialise(CONTEXT *);
void hook_IPX_Start_Listening95(CONTEXT *);
void hook_IPX_Shut_Down95(CONTEXT *);
void hook_IPX_Send_Packet95(CONTEXT *);
void hook_IPX_Broadcast_Packet95(CONTEXT *);
void hook_IPX_Get_Outstanding_Buffer95(CONTEXT *);
void hook_IPX_Open_Socket95(CONTEXT *);
void hook_IPX_Close_Socket95(CONTEXT *);
void hook_IPX_Get_Connection_Number95(CONTEXT *);
void hook_IPX_Get_Local_Target95(CONTEXT *);

/* wsock32 hooks */
void hook_socket(CONTEXT *ctx);
void hook_bind(CONTEXT *ctx);
void hook_recvfrom(CONTEXT *ctx);
void hook_sendto(CONTEXT *ctx);
void hook_getsockopt(CONTEXT *ctx);
void hook_setsockopt(CONTEXT *ctx);

/* rest is just for permission relay */
void hook_closesocket(CONTEXT *ctx);
void hook_WSAAsyncSelect(CONTEXT *ctx);

BOOL FileExists(const char *path);
int cncnet_launch(const char *gameExe, const char *gameParams);
uint32_t pop(CONTEXT *ctx);
void push(CONTEXT *ctx, uint32_t val);
uint32_t memread(void *addr, void *buf, int len);
uint32_t memwrite(void *addr, void *buf, int len);

#endif
